package com.sddz.pacs.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.sddz.pacs.annotation.SysLogger;
import com.sddz.pacs.dto.JsonResult;
import com.sddz.pacs.dto.PageModelParams;
import com.sddz.pacs.entity.HealthUser;
import com.sddz.pacs.entity.SysStation;
import com.sddz.pacs.entity.SysUser;
import com.sddz.pacs.entity.UserResult;
import com.sddz.pacs.service.HealthUserService;
import com.sddz.pacs.service.SysStationService;
import com.sddz.pacs.service.SysUserService;
import com.sddz.pacs.service.UserResultService;
import com.sddz.pacs.support.ResultConstant;
import com.sddz.pacs.util.DateUtil;
import com.sddz.pacs.util.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author SunLi
 * @Date 2018/9/10
 */
@RestController
@RequestMapping("/station")
@Api(description = "服务站相关接口")
public class StationController {

    @Autowired
    private HealthUserService healthUserService;

    @Autowired
    private UserResultService userResultService;

    @PostMapping(value = "/getAllResidentList")
    @ApiOperation(value="服务站所有居民", notes="查询当前服务站所有居民列表")
    @SysLogger(value = "查看所有居民信息",type = "查看所有居民日志管理")
    @ApiImplicitParam(name = "pageModelParams",value = "居民用户json对象",required = true)
    public JsonResult getPersonList(@RequestBody PageModelParams pageModelParams) throws Exception {
        PageInfo<HealthUser> pageInfo = healthUserService.selectHealthUserList(pageModelParams);
        return JsonResult.success(pageInfo,ResultConstant.LIST_SUCCESS);
    }

    @GetMapping(value = "/getByIdResidentList/{id}")
    @ApiOperation(value="服务站居民", notes="查询居民记录")
    @SysLogger(value = "查看所有居民信息",type = "查看所有居民记录日志")
    @ApiImplicitParam(name = "id",value = "居民用户json对象",required = true)
    public JsonResult getByIdResidentList(@PathVariable("id") String id ) throws Exception {
        EntityWrapper<UserResult> ew = new EntityWrapper<>();
        ew.eq("USER_ID",id);
        List<UserResult> healthUser = userResultService.selectList(ew);
        return JsonResult.success(healthUser,ResultConstant.RESIDENT_CHECK_RECORD_SUCCESS);
    }
    @GetMapping(value = "/getByIdResident/{id}")
    @SysLogger(value = "查看居民个人信息",type = "查看所有居民个人信息")
    @ApiOperation(value="居民个人信息", notes="查询当前个人信息")
    @ApiImplicitParam(name = "id",value = "居民用户json对象",required = true)
    public JsonResult getByIdResident(@PathVariable("id") String id ) throws Exception {
        EntityWrapper<HealthUser> ew = new EntityWrapper<>();
        ew.eq("ID",id);
       HealthUser hHealthUser = healthUserService.selectOne(ew);
        return JsonResult.success(hHealthUser,ResultConstant.RESIDENT_MESS_SUCCESS);
    }
    @PostMapping(value = "/getPersonalDate")
    @SysLogger(value = "查看居民一条信息详情",type = "居民个人一条信息详情日志")
    @ApiOperation(value="服务站居民个人一条信息详情", notes="查询居民个人信息详情")
    @ApiImplicitParam(name = "id",value = "居民用户json对象",required = true)
    public JsonResult getPersonalDate(@RequestBody UserResult user ) throws Exception {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Map<String,Object> map = new HashMap<>();
        //通过个人信息获取所有的检查记录
        UserResult userResult =  userResultService.selectPersonalOne(user.getId(),formatter.format(user.getCheckDate()));
        if (userResult != null) {
            map.put("bAddr1", userResult.getbAddr1());
            map.put("bAddr2", userResult.getbAddr2());
            map.put("provingTime", userResult.getCheckDate());
            map.put("remark", userResult.getRemark());
            map.put("userId", userResult.getUserId());
            map.put("fileNumber", userResult.getFileNumber());
            EntityWrapper<HealthUser> ew1 = new EntityWrapper<>();
            ew1.eq("ID", user.getId());
            //获取个人信息
            HealthUser healthUser = healthUserService.selectOne(ew1);
            map.put("age", healthUser.getAge());
            map.put("createDate", healthUser.getCreateDate());
            map.put("gender", healthUser.getGender());
            map.put("homeAddr", healthUser.getHomeAddr());
            map.put("name", healthUser.getName());
            map.put("idCard", healthUser.getIdCard());
            map.put("id", healthUser.getId());
            map.put("stationId", healthUser.getStationId());
            map.put("tel", healthUser.getTel());
            return JsonResult.success(map, ResultConstant.DETAILS_MESS_SUCCESS);
        }else {
            return JsonResult.failure(ResultConstant.DETAILS_MESS_FAILURE);
        }
    }

    @PostMapping(value = "/index")
    @ApiOperation(value="首页", notes="首页")
    @ApiImplicitParam(name = "",value = "居民用户json对象",required = true)
    public JsonResult index() throws Exception {
        Map healthUser = null;
        //获取当前登陆场对象
        SysUser sysUser= ShiroUtils.getUserEntity();
        if(sysUser.getLevels()!=1){
            //服务站获取数据
            healthUser = healthUserService.indexNumber(sysUser.getStationId());
        }else {
            //admin 获取所有数据
            healthUser=  healthUserService.mangerNumber();

        }
        return JsonResult.success(healthUser,ResultConstant.RESIDENT_MESS_SUCCESS);

    }
    @PostMapping(value = "/getByManWomanNum")
    @ApiOperation(value="首页统计男女", notes="查询首页所有男女数量")
    @ApiImplicitParam(name = "",value = "居民用户json对象",required = true)
    public JsonResult getByManWomanNum() throws Exception {
        //获取当前登陆场对象
        SysUser sysUser= ShiroUtils.getUserEntity();
        Map<String,Object>  num = new HashMap<>();
        if(sysUser.getLevels()!=1){
            num = healthUserService.getByManWomanNum(sysUser.getStationId());
        }else{
            num = healthUserService.getManger();
        }

        return JsonResult.success(num,ResultConstant.WAN_WOMAN_NUM_SUCCESS);
    }
    @PostMapping(value = "/getDayData")
    @ApiOperation(value="统计本日所有检查居民列表", notes="统计本日所有居民检查列表")
    @ApiImplicitParam(name = "",value = "居民用户json对象",required = true)
    public JsonResult getDayData() throws Exception {
        //获取当前登陆场对象
        SysUser sysUser= ShiroUtils.getUserEntity();
        List<HealthUser>  num= null;
        if(sysUser.getLevels()!=1){
           num = healthUserService.getDayData(sysUser.getStationId());
            return JsonResult.success(num,ResultConstant.DAY_DATA_LIST);
       }else{
           num=healthUserService.getThreeDays();
            return JsonResult.success(num,ResultConstant.NEARLY_THREE_DAYS);
        }

    }
    @PostMapping(value = "/getYearEveryMonth")
    @ApiOperation(value="统计一年中每个月数据", notes="统计一年中每个月数据")
    @ApiImplicitParam(name = "",value = "居民用户json对象",required = true)
    public JsonResult getYearEveryMonth() throws Exception {
        //获取当前登陆场对象
        SysUser sysUser= ShiroUtils.getUserEntity();
        if(sysUser.getLevels()!=1){
                Map   num = healthUserService.getYearEveryMonth(sysUser.getStationId());
                return JsonResult.success(num,ResultConstant.GET_YEAR_EVERY_MONTH_SUCCESS);
            }else {
                List<Map<String,Object>> num = healthUserService.getLastData();
                return JsonResult.success(num,ResultConstant.LAST_STATION_DATA_SUCCESS);
            }
    }
    @PostMapping(value = "/getSevenDay")
    @ApiOperation(value="统计七天数据", notes="服务站统计七天数据")
    @ApiImplicitParam(name = "",value = "居民用户json对象",required = true)
    public JsonResult getSevenDay()throws Exception {
        //获取当前登陆场对象
        SysUser sysUser= ShiroUtils.getUserEntity();
        List<Map>  map = null;
        if(sysUser.getLevels()!=1){

            map = healthUserService.getSevenDay(sysUser.getStationId());
            return JsonResult.success(map,ResultConstant.GET_SEVEN_DAY_SUCCESS);
        }else{
            map = healthUserService.getMangerSevenDay();
            return JsonResult.success(map,ResultConstant.STATION_DATA_SUCCESS);
        }
    }
    @PostMapping(value = "/getAllList")
    @SysLogger(value = "桥西区所有居民",type = "桥西区所有居民日志管理")
    @ApiOperation(value="桥西区所有居民", notes="查询桥西区所有居民列表")
    @ApiImplicitParam(name = "pageModelParams",value = "居民用户json对象",required = true)
    public JsonResult getAllList(@RequestBody PageModelParams pageModelParams) throws Exception {
        PageInfo<HealthUser> pageInfo = healthUserService.selectAllHealthUserList(pageModelParams);
        return JsonResult.success(pageInfo,ResultConstant.ALL_RESIDENTS_SUCCESS);
    }

}
